<template>
  <div>
    <div class="risk">
      <div class="add" @click="edit()">新增记录</div>
      <eagle-page-list
        :isShowSearch="false"
        :isSticky="false"
        ref="eaglePageList"
        @bindData="bindData"
        :queryParams="queryParams"
        :url="url"
        :conditions="conditions"
        :pageSize="20"
      >
        <template slot="list">
          <eagle-cell
            style="margin-top: 10px"
            v-for="(item, index) in list"
            :key="index"
          >
            <div class="warn_item">
              <div>
                <div class="warn_header">
                  <div>{{ item.EquipmentName }}</div>
                  <eagle-tag
                    v-if="item.ExpireDays < 0"
                    class="warn_tag"
                    type="warning"
                    >剩{{ Math.abs(item.ExpireDays) }}</eagle-tag
                  >
                  <eagle-tag
                    v-if="item.ExpireDays > 0"
                    class="warn_tag"
                    type="warning"
                    >逾期{{ Math.abs(item.ExpireDays) }}</eagle-tag
                  >
                </div>
                <div class="dep_sort">
                  <img src="@/assets/Images/AppIcon/icon_number.png" alt="" />
                  <div>编号：{{ item.EquipmentNo }}</div>
                </div>
                <div class="dep_sort">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_user.png"
                    alt=""
                  />
                  <div>检查人：{{ item.CreateChnName }}</div>
                </div>
                <div class="dep_sort">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_date.png"
                    alt=""
                  />
                  <div>最近检查日期：{{ item.CheckDate | getDateTime() }}</div>
                </div>
                <div class="dep_sort">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_date.png"
                    alt=""
                  />
                  <div>
                    下次校验日期：{{ item.NextCheckDate | getDateTime() }}
                  </div>
                </div>
              </div>
              <van-divider />
              <div class="dep_btm">
                <div class="btm_item" @click="edit(item)">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_edit.png"
                    alt=""
                  />
                  <div class="mr-20">编辑</div>
                </div>
                <div class="btm_item" @click="handleDelete(item)">
                  <img
                    src="@/assets/Images/AppIcon/icon_list_delete.png"
                    alt=""
                  />
                  <div class="mr-10">删除</div>
                </div>
              </div>
            </div>
          </eagle-cell>
        </template>
      </eagle-page-list>
    </div>
    <eagle-dialog
      v-model="show"
      :title="title"
	  :titlePosition="'center'"
      @confirm="handleConfirm()"
      show-cancel-button
	  show-confirm-button
    >
      <eagle-form @submit="onSubmit">
		<eagle-input
			name="CheckDate"
			:value="form.CheckDate"
			label="最近检验日期"
			readonly
			placeholder="请选择"
			islink
			@click="handleShow('CheckDate')"
		></eagle-input>
		<eagle-input
			name="NextCheckDate"
			:value="form.NextCheckDate"
			label="下次检验日期"
			readonly
			placeholder="请选择"
			islink
			@click="handleShow('NextCheckDate')"
		></eagle-input>
      </eagle-form>
    </eagle-dialog>
    <eagle-calendar
      color="#1989fa"
      :min-date="new Date('2000-01-01')"
      :max-date="new Date('2040-01-01')"
      v-model="showCalendar"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { Dialog } from "vant";
import { formatDate, getDate, getDateTime } from "@/utils/getDate";
export default {
  data() {
    return {
      list: [],
      conditions: {},
      url: "/EquEquipment/GetEquCheckPageData",
      queryParams: {
        dataType: "List",
        equipmentCode: this.$route.query.equipmentCode,
      },
      show: false,
      showCalendar: false,
      selectKey: "",
      value: "",
      form: {},
      title: "",
    };
  },
  filters: {
    getDateTime: function (value) {
      return getDate(value);
    },
  },
  created() {
  },
  methods: {
    bindData(data) {
      this.list = [];
      if (data) {
        this.list = data;
      }
    },
    handleConfirm() {
	  let _this = this;
      _this.form.equipmentCode = _this.$route.query.equipmentCode;
	  _this.$axios
	  .post("/EquEquipment/EditCheckEquipmen",_this.form)
	  .then((res)=>{
		_this.$toast.success("编辑成功！");
		_this.$refs.eaglePageList.search();
	  })
    },
    handleShow(name) {
      this.showCalendar = true;
      this.selectKey = name;
    },
    handleDelete(item) {
      Dialog.confirm({
        title: "询问",
        message: "确定要删除该条附件记录吗?",
      })
        .then(() => {
          // on confirm
		  let _this = this;
		  _this.$axios.
		  get("/EquEquipment/DeleteCheckEquipmen",{params:{id: item.ID}})
		  .then((res)=>{
			_this.$toast.success("删除成功！");
			_this.$refs.eaglePageList.search();
		  })
        })
        .catch(() => {
          // on cancel
        });
    },

    onConfirm(date) {
      const dateTime = formatDate(date);
      this.form[this.selectKey] = getDate(dateTime);
      this.showCalendar = false;
    },

    onSubmit(values) {
      console.log("submit", values);
    },

    edit(obj) {
      if (obj) {
        this.title = "编辑检查历史";
        this.form.CheckDate = getDate(obj.CheckDate);
        this.form.NextCheckDate = getDate(obj.NextCheckDate);
        this.form.ID = obj.ID;
        this.show = true;
      } else {
		let _this = this;
        _this.title = "新增检查历史";
		_this.$axios.get("/EquEquipment/InitCheckEntity",{params:{equpimentCode: _this.$route.query.equipmentCode}})
          .then((res) => {
            _this.form.CheckDate = getDate(res.data.CheckDate);
            _this.form.NextCheckDate = getDate(res.data.NextCheckDate);
            _this.show = true;
          })
          .catch((err) => {
            console.log("err", err);
          });
      }
    },
    // 设备详情
    goDetail(equipmentCode) {
      this.$router.push({
        path: "/Equipment/Detail",
        query: { equipmentCode: equipmentCode },
      });
    },
    // // 编辑设备信息
    // edit() {
    //   this.$router.push("/Equipment/Edit");
    // },
    // 设备检验
    editCheck(equipmentCode) {
      this.$router.push({
        path: "/Equipment/editcheck",
        query: { equipmentCode: equipmentCode },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.risk {
  background-color: #e8f1f8;
  .info_search {
    padding: 10px;
    border-radius: 5px !important;
    .van-field__control {
      background-color: #ffffff !important;
    }
  }
}
.all_dep {
  background-color: #ffffff;
}
.warn_item {
  /*margin-bottom: 10px;*/

  border-radius: 5px;
  position: relative;
}
.warn_header {
  display: flex;
  justify-content: space-between;
  position: relative;
  margin-bottom: 10px;
  .warn_tag {
    position: absolute;
    right: 0;
    top: 0;
    border-radius: 26.64rem 0 0 26.64rem;
  }
}
.dep_sort {
  display: flex;
  align-items: center;
  margin-bottom: 5px;
  img {
    height: 15px;
    width: 15px;
    margin-right: 5px;
  }
}
.dep_btm {
  display: flex;
  justify-content: flex-end;
  .btm_item {
    display: flex;
    align-items: center;
    img {
      height: 15px;
      width: 15px;
      margin-right: 5px;
    }
  }
}
</style>
